<?php

namespace backend\module\shop\controllers;

use Yii;
use backend\module\shop\models\GoodsCategory;
use yii\data\ArrayDataProvider;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \yii\web\Response;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use backend\models\BackendUser;
use backend\components\actions\ChangeValueAction;
/**
 * GoodsCategoryController implements the CRUD actions for GoodsCategory model.
 */
class GoodsCategoryController extends BaseController {

    /**
     * @inheritdoc
     */
    public function behaviors() {
        return ArrayHelper::merge(parent::behaviors(), [
        ]);
    }

    public function actions() {
        return ArrayHelper::merge(parent::actions(), [
                    'change-value' => [
                        'class' => ChangeValueAction::className(),
                        'modelClass' => GoodsCategory::className(),
                        'outputValue' => function($model, $attribute, $key, $index) {
                            return $model->$attribute;
                        }
                    ],
        ]);
    }

    /**
     * Lists all GoodsCategory models.
     * @return mixed
     */
    public function actionIndex() {
        $data = GoodsCategory::getAllData(BackendUser::getParentUserId());
        $gridData = GoodsCategory::getNodeGrid($data);
        $dataProvider = new ArrayDataProvider([
            'allModels' => $gridData,
            'pagination' => false,
        ]);

        return $this->render('index', [
                    'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single GoodsCategory model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id) {
        $request = Yii::$app->request;
        Yii::$app->response->format = Response::FORMAT_JSON;
        return [
            'title' => Yii::t('common', 'View'),
            'content' => $this->renderAjax('view', [
                'model' => $this->findModel($id),
            ]),
            'footer' => Html::button(Yii::t('common', 'Close'), ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
            Html::a(Yii::t('common', 'Edit'), ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
        ];
    }

    /**
     * Creates a new GoodsCategory model.
     * For ajax request will return json object
     * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate() {
        $request = Yii::$app->request;
        $nodeData = GoodsCategory::getAllData(BackendUser::getParentUserId());
        $model = new GoodsCategory();
        $model->sort = Yii::$app->params['defaultSortNum'];
        $model->userid = BackendUser::getParentUserId();
        Yii::$app->response->format = Response::FORMAT_JSON;
        if ($request->isPost && $model->load($request->post()) && $model->save()) {
            return [
                'forceReload' => '#crud-datatable-pjax',
                'title' => Yii::t('common', 'Create'),
                'content' => '<span class="text-success">' . Yii::t('common', 'Create success') . '</span>',
                'footer' => Html::button(Yii::t('common', 'Close'), ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                Html::a(Yii::t('common', 'Create More'), ['create'], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
            ];
        }
        return [
            'title' => Yii::t('common', 'Create'),
            'content' => $this->renderAjax('create', [
                'model' => $model,
                'selectData' => GoodsCategory::getNodeSelect($nodeData),
            ]),
            'footer' => Html::button(Yii::t('common', 'Close'), ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
            Html::button(Yii::t('common', 'Save'), ['class' => 'btn btn-primary', 'type' => "submit"])
        ];
    }

    /**
     * Updates an existing GoodsCategory model.
     * For ajax request will return json object
     * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id) {
        $request = Yii::$app->request;
        $model = $this->findModel($id);
        $nodeData = GoodsCategory::getAllData(BackendUser::getParentUserId());
        Yii::$app->response->format = Response::FORMAT_JSON;
        if ($request->isPost && $model->load($request->post()) && $model->save()) {
            return [
                'forceReload' => '#crud-datatable-pjax',
                'title' => Yii::t('common', 'Update'),
                'content' => $this->renderAjax('view', [
                    'model' => $model,
                ]),
                'footer' => Html::button(Yii::t('common', 'Close'), ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
                Html::a(Yii::t('common', 'Edit'), ['update', 'id' => $id], ['class' => 'btn btn-primary', 'role' => 'modal-remote'])
            ];
        }
        return [
            'title' => Yii::t('common', 'Update'),
            'content' => $this->renderAjax('update', [
                'model' => $model,
                'selectData' => GoodsCategory::getNodeSelect($nodeData),
            ]),
            'footer' => Html::button(Yii::t('common', 'Close'), ['class' => 'btn btn-default pull-left', 'data-dismiss' => "modal"]) .
            Html::button(Yii::t('common', 'Save'), ['class' => 'btn btn-primary', 'type' => "submit"])
        ];
    }

    /**
     * Delete an existing GoodsCategory model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id) {
        $request = Yii::$app->request;
        $this->findModel($id)->delete();

        Yii::$app->response->format = Response::FORMAT_JSON;
        return ['forceClose' => true, 'forceReload' => '#crud-datatable-pjax'];
    }

    /**
     * Delete multiple existing GoodsCategory model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionBulkDelete() {
        $request = Yii::$app->request;
        $pks = explode(',', $request->post('pks')); // Array or selected records primary keys
        foreach ($pks as $pk) {
            $model = $this->findModel($pk);
            $model->delete();
        }

        Yii::$app->response->format = Response::FORMAT_JSON;
        return ['forceClose' => true, 'forceReload' => '#crud-datatable-pjax'];
    }

    /**
     * Finds the GoodsCategory model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return GoodsCategory the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id) {
        if (($model = GoodsCategory::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }

}
